<template>
    <van-overlay :z-index="9999" :show="isShow" @click="closeHandle" :lock-scroll="false">
        <div class="user-box" @click.stop>
            <div class="box-border">
                <div class="box-title">
                    <div><a @click="closeHandle">{{ $t('circle_kg_13') }}</a></div>
                    <div>{{ $t('select_user') }}</div>
                    <div><a :class="{'btn-class': selectIdList.length > 0}" @click="changeSelect">{{ $t('circle_kg_14') }}</a></div>
                </div>
                <div>
                    <van-search v-model="search" disabled :placeholder="$t('cur_search_not')" />
                </div>
                <div class="box-item-list">
                    <van-checkbox-group v-model="selectIdList">
                        <div class="box-item" v-for="data in scopeUserList" :key="data.id">
                            <div><van-checkbox :name="data.id"></van-checkbox></div>
                            <div @click="selectUser(data)">{{ data.name }}</div>
                        </div>
                    </van-checkbox-group>
                    <div v-if="!isLoading && scopeUserList.length === 0" style="height:100%;">
                        <div class="box-table" style="width:100%;height:100%;">
                            <div class="box-table-cell" style="text-align: center;">
                                <div class="no-data">
                                    <img src="../../../static/images/no-data.png" alt=""/>
                                    <p class="color999">{{ $t("no_data") }}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </van-overlay>
</template>

<script>
import { Toast, Image, Dialog, Checkbox, CheckboxGroup, Search, Tab, Tabs, SwipeCell, Button, Icon, Overlay, Field, Collapse, CollapseItem, Loading } from 'vant';
export default {
    name: 'ScopeSelectUser',
    components: {
        vatToast: Toast,
        vanImage: Image,
        vanDialog: Dialog,
        vanCheckbox: Checkbox,
        vanCheckboxGroup: CheckboxGroup,
        vanOverlay: Overlay,
        vanCollapse: Collapse,
        vanCollapseItem: CollapseItem,
        vanLoading: Loading,
        vanSearch: Search
    },
    data() {
        return {
            selectIdList: [],
            search: '',
            isLoading: false
        };
    },
    props: {
        isShow: {
            type: Boolean,
            default: false
        },
        scopeUserList: {
            type: Array,
            default() {
                return [];
            }
        },
        defaultSelectedIdList: {
            type: Array,
            default() {
                return [];
            }
        }
    },
    methods: {
        selectUser(data) {
            let index = this.selectIdList.indexOf(data.id);
            if (index === -1) {
                this.selectIdList.push(data.id);
            } else {
                this.selectIdList.splice(index, 1);
            }
        },
        closeHandle() {
            this.selectIdList = [];
            this.$emit('update:isShow', !this.isShow);
            this.$emit('close', !this.isShow);
            this.isLoading = false;
        },
        changeSelect() {
            if (!this.isLoading && this.selectIdList.length > 0) {
                this.isLoading = true;
                let objList = [];
                this.selectIdList.forEach(id => {
                    for (let i = 0; i < this.scopeUserList.length; i++) {
                        let item = this.scopeUserList[i];
                        if (item.id === id) {
                            objList.push({ id: id, name: item.name });
                            break;
                        }
                        if (i + 1 === this.scopeUserList.length) {
                            objList.push({ id: id, name: 'Not Name' });
                        }
                    }
                });
                this.$emit('changeIdList', this.selectIdList);
                this.$emit('changeObjList', objList);
                this.closeHandle();
            }
        },
        defaultHandle() {
            this.selectIdList = [].concat(this.defaultSelectedIdList);
        }
    },
    watch: {
        isShow() {
            if (this.isShow) {
                if (this.defaultSelectedIdList.length > 0) {
                    this.defaultHandle();
                }
            }
        },
        defaultSelectedIdList() {
            this.defaultHandle();
        }
    }
};
</script>

<style scoped lang="less">
    /deep/.van-overlay {
        z-index: 999 !important;
    }
    /deep/.downLoading {
        background: #fff;
    }
    .user-box {
        background: #fff;
        position: absolute;
        bottom: 0;
        width: 100%;
        height: 90%;
        border-radius: 16px 16px 0 0;
        .box-border {
            padding: 0 15px;
            height: 100%;
            .box-title {
                height: 48px;
                display: flex;
                align-items: center;
                &>div:first-child{
                    flex: 1;
                    &>a {
                        padding-right: 10px;
                    }
                }
                &>div:last-child {
                    flex: 1;
                    text-align: right;
                    &>a {
                        padding-left: 10px;
                        color: #ACACAC;
                    }
                }
                &>div:nth-child(2) {
                    flex: 2;
                    text-align: center;
                }
                .btn-class {
                    color: #000 !important;
                }
            }
            /deep/.van-search {
                padding: 0 0 10px 0;
            }
            .box-item-list {
                overflow: hidden;
                height: calc(100% - 92px);
                .over-hidden {
                    overflow: hidden;
                }
                /deep/.van-loading {
                    text-align: center;
                    height: 100%;
                }
                /deep/.van-loading__spinner {
                    height: calc(100% - 100px) !important;
                }
                .box-item {
                    display: flex;
                    align-items: center;
                    height: 50px;
                    &>div:first-child {
                        width: 50px;
                    }
                    &>div:last-child {
                        flex-grow: 1;
                    }
                    /*border-bottom: 1px #ACACAC solid;*/
                    /*&:last-child {*/
                    /*    border: none;*/
                    /*}*/
                }
            }
        }
    }
</style>
